package org.jeecg.modules.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.impl.identity.Authentication;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.dto.ActProcessInstDto;
import org.jeecg.modules.service.IActProcessInstService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "流程实例")
@RestController
@RequestMapping("/act/procinst")
@Slf4j
public class ActProcessInctController extends JeecgController {

    @Autowired
    private IActProcessInstService service;

    /**
     * 分页列表查询
     *
     * @param instance
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "流程实例-分页列表查询")
    @ApiOperation(value = "流程实例-分页列表查询", notes = "流程实例-分页列表查询")
    @GetMapping(value = "/listPage")
    public Result<IPage<ActProcessInstDto>> listPage(ProcessInstance instance,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     HttpServletRequest req) {
        IPage<ActProcessInstDto> list = service.listPage(instance, pageNo, pageSize);
        return Result.OK(list);
    }

    @ApiOperation(value = "获取流程实例", notes = "根据实例Id，获取流程实例详情")
    @GetMapping(value = "/getProcInstance")
    public Result<ActProcessInstDto> getProcInstance(@RequestParam("procInsId") String procInsId) throws Exception {
        return Result.ok(service.getProcInstance(procInsId));
    }

    @ApiOperation(value = "启动流程", notes = "启动流程")
    @GetMapping(value = "/start")
    @PermissionData("act:process:start")
    public Result<ActProcessInstDto> start(@RequestParam("processKey") String processKey,
                                         @RequestParam("instanceName") String instanceName,
                                         HttpServletRequest request) {
        Map<String, Object> variables = new HashMap<>();
        String username = JwtUtil.getUserNameByToken(request);
        Authentication.setAuthenticatedUserId(username);
        // variables.put("startUserKey", username);
        final String processInstanceId = service.startProcess(processKey, "", "", instanceName, variables);
        return Result.OK("启动流程成功！processInstanceId=" + processInstanceId);
    }


    /**
     * 通过id删除
     *
     * @param procInstId
     * @return
     */
    @AutoLog(value = "流程实例-通过procInstId删除")
    @ApiOperation(value = "流程模板-通过id删除", notes = "流程模板-通过id删除")
    @RequiresPermissions("act:process:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "procInstId", required = true) String procInstId) {
        service.delete(procInstId);
        return Result.OK("删除成功!");
    }

    @AutoLog(value = "流程实例-根据procInstId设置流程状态")
    @RequiresPermissions("act:process:updateState")
    @GetMapping(path = "/updateState")
    @ApiOperation(value = "设置流程状态", notes = "根据procInstId设置流程状态")
    public Result<String> updateState(@RequestParam("procInstId") String procInstId, @RequestParam("suspended") Boolean suspended) {
        String msg = service.updateState(procInstId, suspended);
        return Result.OK(msg);
    }
}
